<template>
  <div id="home">
      <div class="b1"></div>
      <div class="b2" id="b2">
        <!-- 公告区 -->
        <div class="guangbo">
          <div class="guangbo1"></div>
          <div class="guangbo2" style="color: #ed6c23; font-weight: 600">{{useNumber}}</div>
          <div class="guangbo3"></div>
        </div>
        <!-- 提交表单区 -->
        <div class="apply-for-form"> 
          <van-form @submit="applyForSubmit">
            <div class="form-input">
              <van-cell-group>
                <img width="25" height="25" src="../../assets/images/yzszh/lianxiren.png" />
                <van-field class="form-field"  size="small" v-model="contact" label-width="7em" label="姓名：" placeholder="必填，请输入" input-align="right"/>
              </van-cell-group>
            </div>
            <div class="form-input">
              <van-cell-group>
                <img width="25" height="25" src="../../assets/images/yzszh/dianhua.png" />
                <van-field class="form-field"  size="small" v-model="phone" label-width="7em" label="电话：" type="number" maxlength="12" placeholder="必填，请输入" input-align="right"/>
              </van-cell-group>
            </div>
            <div class="form-input">
              <van-cell-group>
                <img width="25" height="25" src="../../assets/images/yzszh/qiye.png" />
                <van-field class="form-field" size="small" v-model="name" readonly  label-width="7em" label="公司名称：" placeholder="必填，请输入" input-align="right" @click="show = true"/>
              </van-cell-group>
            </div>
            <div class="form-input">
              <van-cell-group>
                <img width="25" height="25" src="../../assets/images/yzszh/tongji.png" />
                <van-field class="form-field"  size="small" v-model="industry" label-width="7em" label="所属行业：" placeholder="选填" input-align="right"/>
              </van-cell-group>
            </div>
            <van-button class="form-button" type="info" block native-type="submit"></van-button>
          </van-form>
        </div>
      </div>
      <div class="b3"></div>
      <div style=" position: -webkit-sticky; position: sticky; bottom: 0.05rem; height: 43px">
        <a :href="'tel:4006 021 808'">
          <van-button square type="primary" class="phone"></van-button>
        </a>
        <van-button square type="info" class="jump" @click="jump"></van-button>
      </div>
      <div v-show="show" class="popup" @click="show = false; searchValue = ''" ref="popup">
        <div @click.stop>
          <van-search v-model="searchValue" placeholder="请输入搜索关键词" />
          <div style="background-color: #fff; height: 5rem; overflow: hidden; overflow-x: hidden; overflow-y: scroll;">
            <van-list v-model="loading" :finished="finished" finished-text="天眼查没有更多了">
              <van-cell-group :title="item.groupName" v-for="item in list" :key="item.id" @click="trigger(item.id)">
                <van-cell :key="item.id" :title="item.title" :label="item.label"/>
              </van-cell-group>
            </van-list>
          </div>
        </div>
      </div>
  </div>
</template>

<script>
  import { Form, Button, Field, Cell, CellGroup, List, Search  } from 'vant'
  export default {
    name: 'YzszhHome',
    data() {
      return {
        spreadUserId: '',
        customerSource: '',
        jumpFlag: false,
        show: false,
        list: [],
        loading: false,
        finished: false,
        contact: '',
        phone: '',
        name:'',
        industry: '',
        useNumber: 0,
        searchValue: '',
        scroll: '',
        browser: ''
      }
    },
    components: {
      'van-form': Form,
      'van-cell-group': CellGroup,
      'van-list' : List,
      'van-cell': Cell,
      'van-field': Field,
      'van-button': Button,
      'van-search': Search
    },
    activated () {
      this.getIP()
      this.getUseNumber()
    },
    mounted(){
      this.browser = localStorage.getItem('browser')
      this.browser = JSON.parse(this.browser)
      console.log(this.browser)
      if (this.browser.os != 'iOS') {
        this.scroll = document.documentElement.scrollTop || document.body.scrollTop;
        this.$refs.popup.style.paddingTop = this.scroll  + 'px'
      } else {
        this.$refs.popup.style.paddingTop = '0rem'
      }
      window.addEventListener('scroll', this.scrollTop)
    },
    destroyed(){
      window.removeEventListener('scroll',this.scrollTop)
    },
    methods: {
      scrollTop(){
        this.browser = localStorage.getItem('browser')
        this.browser = JSON.parse(this.browser)
        if (this.browser.os != 'iOS') {
          this.scroll = document.documentElement.scrollTop || document.body.scrollTop;
          this.$refs.popup.style.paddingTop = this.scroll  + 'px'
        } else {
          this.$refs.popup.style.paddingTop = '0rem'
        }
      },
      getIP () {
        var queryArray = this.getQueryString()
        this.spreadUserId = queryArray['spreadUserId']
        this.customerSource = queryArray['customerSource']
        if (this.spreadUserId && this.customerSource) {
          localStorage.setItem('spreadUserId', this.spreadUserId)
          localStorage.setItem('customerSource', this.customerSource)
          var parm = {
            eventId: "3",
            ip: localStorage.getItem('Ip'),
            address: localStorage.getItem('cityname'),
            browser: localStorage.getItem('browser'),
            spreadUserId: this.spreadUserId,
            customerSource: this.customerSource
          }
          var url = this.HOME + 'api/h5/EqxiuVisit/visit';
          this.$axios({
            method: 'post',
            url: url,
            data: {entity:parm}
          }).then((res) => {
            if (res.data.code === 200) {
              this.jumpFlag = true
            }
          }).catch((err) => {
            this.jumpFlag = false
            this.$toast.fail('网络异常')
          }) 
        } else {
          this.jumpFlag = false
          this.$toast.fail('非常抱歉，我们检测不到推广人员信息和来源信息！')
        }
      },
      getUseNumber () {
        var url = this.HOME + 'api/h5/EqxiuVisit/getRegisterCountByEventId?eventId=3';
        this.$axios({
          method: 'get',
          url: url
        }).then((res) => {
          if (res.data.code === 200) {
            this.useNumber = res.data.data
          }
        }).catch((err) => {
          this.$toast.fail('网络异常')
        }) 
      },
      jump () {
        document.querySelector('#b2').scrollIntoView({ behavior:'smooth' })
      },
      applyForSubmit(){
        if (this.jumpFlag) {
          if (this.contact && this.phone && this.name) {
            if (this.phone.length === 11 || this.phone.length === 12) {
              var parm = {
                eventId: "3",
                contact: this.contact.toString(),
                phone: this.phone.toString(),
                name: this.name.toString(),
                companyInfo: this.companyInfo,
                customer_industry: this.industry.toString(),
                customerSource: localStorage.getItem('customerSource'),
                spreadUserId: localStorage.getItem('spreadUserId'),
              }
              var url = this.HOME + 'api/h5/EqxiuVisit/apply';
              this.$axios({
                method: 'post',
                url: url,
                data: {entity:parm}
              }).then((res) => {
                if (res.data.code === 200) {
                  this.$toast.fail('提交成功！')
                }
              }).catch((err) => {
                this.$toast.fail('网络异常！')
              }) 
            } else {
              this.phone = "";
              this.$toast.fail('联系方式不正确请重新输入！')
            }
          } else {
            this.$toast.fail('数据有缺失项，请补全在提交！')
          }
        } else {
          this.$toast.fail('非常抱歉，我们检测不到推广人员信息和来源信息！')
        }
      },
      getMessage (value) {
        var that = this
        that.loading = true
        that.list = []
        var url = this.HOME + 'api/h5/EqxiuVisit/queryCompanyInfoByName?name=' + value;
        this.$axios({  
          method: 'get',
          url: url,
        }).then((res) => {
          if (res.data.code === 200 && res.data.data.result) {
            var resultData = res.data.data.result.items;
            that.companyArray = resultData
            for (var i=0; i<resultData.length; i++) {
              var str = resultData[i].name
              str = str.replace(/<em>/g,'')
              str = str.replace(/<\/em>/g,'')
              var json = {
                id: i,
                groupName: '企业名称：' + str,
                title: '法人：' + resultData[i].legalPersonName, 
                label: '注册时间：' + resultData[i].estiblishTime
              }
              that.list.push(json)
            }
          }
          that.loading = false;
          that.finished = true;
        }).catch((err) => {
          console.log(err);
        })
      },
      trigger(index) {
        var groupName = this.list[index].groupName
        this.name = groupName.split('：')[1]
        this.companyInfo = JSON.stringify(this.companyArray[index])
        this.show = false;
      },
      getQueryString() {
        const url = location.href; //获取url中"?"符后的字串
        let theRequest = new Object()
        if (url.indexOf("?") != -1) {
            let str = url.split('?')
            str = str[1].split("&")
            for(let i = 0; i < str.length; i ++) {
              theRequest[str[i].split("=")[0]]=unescape(str[i].split("=")[1])
            }
        }
        return theRequest;
      }
    },
    watch: {
      searchValue: function(value) {
        this.getMessage(value);
      }
    }
  }
</script>

<style scoped>
  #home {

  }

  .b1 {
    background: url('../../assets/images/yzszh/b1.png') no-repeat 100% 0;
    background-size: 100%;
    height: 6.6rem;
  }

  .b2 {
    background: url('../../assets/images/yzszh/b2.png') no-repeat 100% 0;
    background-size: 100%;
    height: 3.5rem;
  }

  .b3 {
    background: url('../../assets/images/yzszh/b3.png') no-repeat 100% 0;
    background-size: 100%;
    height: 17rem;
  }

  .popup {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 2001;
    width: 100%;
    height: 21.58rem;
    background-color: rgba(0, 0, 0, 0.7);
    padding-top: 0rem
  }

  .guangbo {
    padding-left: 2em;
    padding-top: 2em;
  }

  .guangbo1 {
    background: url('../../assets/images/yzszh/btn-guangbo.png') no-repeat 100% 0;
    background-size: 100%;
    height: 4em;
    width: 43%;
    float: left;
  }

  .guangbo2 {
    margin-left: 5px;
    margin-right: 5px;
    padding-top: 0.5em;
    float: left;
    color: #ffffff;
  }

  .guangbo3 {
    background: url('../../assets/images/yzszh/btn-guangbo2.png') no-repeat 100% 0;
    background-size: 100%;
    height: 4em;
    width: 34%;
    float: left;
  }

  .apply-for-form {
    width: 3.5rem;
    margin-left: 0.9em;
  }

  .apply-for-form .form-input {
    width: 94%;
    height: 40px;
    background: #ffffff;
    border-radius: 8px;
    margin: 0 auto;
    margin-bottom: 1em;
    overflow: hidden;
    border: 1px solid #dcdfe6;
 
  }

  .apply-for-form .form-input  img {
    margin-left: 12px; 
    padding-top: 7px;
    vertical-align: middle; 
  }

  .apply-for-form .form-input .form-field {
    padding: 10px 0px;
    left: 48px;
    top: -33px;
    width: 80%;
  }
  
  .apply-for-form .form-button {
    width: 95%;
    margin: 0 auto;
    margin-top: 0rem;
    background: url('../../assets/images/yzszh/btn-baoming.png') no-repeat 50% 50%;
    background-size: 100%;
    border: none;
  }

  .phone {
    width: 1rem;
    background: url('../../assets/images/yzszh/btn-phone.png') no-repeat 100% 0;
    background-size: 100%;
    border: none;
    height: 0.45rem;
  }

  .jump {
    width: 2.75rem;
    background: url('../../assets/images/yzszh/btn-yuyue.png') no-repeat 100% 0;
    background-size: 100%;
    border: none;
    height: 0.45rem;
  }
</style>
